<?php
/**
 * <https://y.st./>
 * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

$xhtml = array(
	'<{title}>' => 'Forking',
	'<{subtitle}>' => 'Written in <span title="Operating Systems 1">CS 2301</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-09-20',
	'<{copyright year}>' => '2017',
	'takedown' => '2017-11-01',
	'<{body}>' => <<<END
<p>
	The fork() $a[API] function provided by the operating system is used to fork processes.
	For this assignment, we were asked to call this function in a program.
	A program needs to be written in a language though, and the language used in the examples provided by the textbook are written in C.
	C isn&apos;t a language I&apos;m fluent in, and it isn&apos;t a language we&apos;ve covered in our studies yet.
	The prerequisites for this course involved Python and Java, but not C.
	As we don&apos;t seem to be using any particular language for this course and I don&apos;t know the language the examples are written in, I&apos;ve decided to use my native tongue: $a[PHP].
</p>
<p>
	The operating system has a name for each of its $a[API] functions, but the languages that code can be written in may use other function names.
	The compiler or interpreter makes the translation as necessary.
	I can&apos;t seem to find a way to access this functionality in Java.
	In Python, it&apos;s available via the <code>os.fork()</code> method.
	In $a[PHP], we call <code>pcntl_fork()</code> to fork a process (The PHP Group, n.d.).
	<code>pcntl_fork()</code> is simply the wrapper $a[PHP] uses for the operating system&apos;s <code>fork()</code> $a[API] function.
</p>
<h2>Code</h2>
<blockquote>
<pre><code><span class="PHP-T_OPEN_TAG">&lt;?php
</span><span class="PHP-T_VARIABLE">\$var</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T">=</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T_LNUMBER">100</span><span class="PHP-T">;</span><span class="PHP-T_WHITESPACE">

</span><span class="PHP-T_SWITCH">switch</span><span class="PHP-T">(</span><span class="PHP-T_STRING">pcntl_fork</span><span class="PHP-T">(</span><span class="PHP-T">)</span><span class="PHP-T">)</span><span class="PHP-T">:</span><span class="PHP-T_WHITESPACE">
	</span><span class="PHP-T_CASE">case</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T">-</span><span class="PHP-T_LNUMBER">1</span><span class="PHP-T">:</span><span class="PHP-T_WHITESPACE">
		</span><span class="PHP-T_ECHO">echo</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T_CONSTANT_ENCAPSED_STRING">&apos;The process failed to fork!&apos;</span><span class="PHP-T">;</span><span class="PHP-T_WHITESPACE">
	</span><span class="PHP-T_BREAK">break</span><span class="PHP-T">;</span><span class="PHP-T_WHITESPACE">
	</span><span class="PHP-T_CASE">case</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T_LNUMBER">0</span><span class="PHP-T">:</span><span class="PHP-T_WHITESPACE">
		</span><span class="PHP-T_ECHO">echo</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T">&quot;</span><span class="PHP-T_ENCAPSED_AND_WHITESPACE">Child process; original value: </span><span class="PHP-T_VARIABLE">\$var</span><span class="PHP-T_ENCAPSED_AND_WHITESPACE">\\n</span><span class="PHP-T">&quot;</span><span class="PHP-T">;</span><span class="PHP-T_WHITESPACE">
		</span><span class="PHP-T_VARIABLE">\$var</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T">=</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T_VARIABLE">\$var</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T">+</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T_LNUMBER">2</span><span class="PHP-T">;</span><span class="PHP-T_WHITESPACE">
		</span><span class="PHP-T_ECHO">echo</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T">&quot;</span><span class="PHP-T_ENCAPSED_AND_WHITESPACE">Child process; updated value: </span><span class="PHP-T_VARIABLE">\$var</span><span class="PHP-T_ENCAPSED_AND_WHITESPACE">\\n</span><span class="PHP-T">&quot;</span><span class="PHP-T">;</span><span class="PHP-T_WHITESPACE">
	</span><span class="PHP-T_BREAK">break</span><span class="PHP-T">;</span><span class="PHP-T_WHITESPACE">
	</span><span class="PHP-T_DEFAULT">default</span><span class="PHP-T">:</span><span class="PHP-T_WHITESPACE">
		</span><span class="PHP-T_ECHO">echo</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T">&quot;</span><span class="PHP-T_ENCAPSED_AND_WHITESPACE">Parent process; original value: </span><span class="PHP-T_VARIABLE">\$var</span><span class="PHP-T_ENCAPSED_AND_WHITESPACE">\\n</span><span class="PHP-T">&quot;</span><span class="PHP-T">;</span><span class="PHP-T_WHITESPACE">
		</span><span class="PHP-T_VARIABLE">\$var</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T">=</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T_VARIABLE">\$var</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T">+</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T_LNUMBER">3</span><span class="PHP-T">;</span><span class="PHP-T_WHITESPACE">
		</span><span class="PHP-T_ECHO">echo</span><span class="PHP-T_WHITESPACE"> </span><span class="PHP-T">&quot;</span><span class="PHP-T_ENCAPSED_AND_WHITESPACE">Parent process; updated value: </span><span class="PHP-T_VARIABLE">\$var</span><span class="PHP-T_ENCAPSED_AND_WHITESPACE">\\n</span><span class="PHP-T">&quot;</span><span class="PHP-T">;</span><span class="PHP-T_WHITESPACE">
</span><span class="PHP-T_ENDSWITCH">endswitch</span><span class="PHP-T">;</span></code></pre>
</blockquote>
<h2>Output</h2>
<blockquote>
<pre>Parent process; original value: 100
Parent process; updated value: 103
Child process; original value: 100
Child process; updated value: 102</pre>
</blockquote>
<h2>Interpretation</h2>
<p>
	When the process first forks, the value of the variable is identical in both the child and the parent.
	However, when the processes change the value after the fork, the values then differ between the two processes.
	Each process has its own copy of the variable, and the copies do not interact in any way.
</p>
<h2>Conclusion</h2>
<p>
	In one way, the results of this exercise surprised me, but in another way, they didn&apos;t.
	Intuitively, I would think that once the process forks, two copies of the variable would exist; one for each copy of the process.
	This is exactly what happened.
	However, the fact that we had an assignment to demonstrate this made me think my hunch about the results would be wrong.
	I thought the assignment was designed to show us that both processes were operating in the same memory space, contrary to intuition.
	However, the assignment backed up my intuition.
	Forking the process creates a separate, duplicate copy of the initial virtual $a[RAM].
</p>
<div class="APA_references">
	<h2>References:</h2>
	<p>
		The PHP Group. (n.d.). PHP: pcntl_fork - Manual. Retrieved from <a href="https://secure.php.net/manual/en/function.pcntl-fork.php"><code>https://secure.php.net/manual/en/function.pcntl-fork.php</code></a>
	</p>
</div>
END
);
